<template>
  <div class="dialog">
    <el-dialog :title="'投料机' + titleText" :visible.sync="showDialog" width="50%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" label-width="120px"
        class="demo-ruleForm">
        <el-form-item label="投料机名称:" prop="feedMachineName">
          <el-input v-model="formData.feedMachineName" placeholder="请输入投料机名称" clearable />
        </el-form-item>
        <el-form-item label="投料机序列号:" prop="deviceId">
          <el-input v-model="formData.deviceId" placeholder="请输入序列号" clearable />
        </el-form-item>
        <el-form-item label="投料机类型:" prop="type">
          <el-select class="w100p" filterable clearable v-model="formData.type" placeholder="请选择">
            <el-option label="一拖一" value="一拖一" />
            <el-option label="一拖二" value="一拖二" />
            <el-option label="一拖三" value="一拖三" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FeedingEquipmentApi from "@/api/feeding-manegement/feeding-equipment";
import DataItemManageApi from "@/api/basic-info-manage/data-item-manage";
import FodderApi from "@/api/basic-info-manage/fodder-management";
import FishPondApi from "@/api/basic-info-manage/fishPond";
export default {
  name: "",
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    feedMachineList: {
      type: Array,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      tempName: undefined,
      formData: {},
      fodderList: [],
      pondList: [],
      dataItemList: [],
      rules: {
        deviceId: [
          {
            required: true,
            message: "投料机序列号不能为空",
            trigger: ["change", "blur"]
          }
        ],
        feedMachineName: [
          {
            required: true,
            message: "投料机名称不能为空",
            trigger: ["change", "blur"]
          }
        ],
        status: [
          { required: true, message: "选择状态", trigger: ["change", "blur"] }
        ],
        type: [
          { required: true, message: "选择类型", trigger: ["change", "blur"] }
        ],
        // rtuId: [
        //   { required: true, message: "选择设备", trigger: ["change", "blur"] }
        // ],
      },
      showDialog: this.show
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
      if (v) {
        this.queryFishPond();
        this.queryDataItem();
        this.queryFodder();
      }
    },
    nowChanceStau(v) {
      if (v === "add") {
        this.tempName = null
        this.formData = {
          status: false,
          materialList: [
            {
              pondList: [{}]
            }
          ]
        };
        this.$nextTick(() => {
          this.$refs["ruleForm"].clearValidate();
        });
      }
      if (v === "edit") {
        this.formData = this.editObject;
      }
    }
  },
  created() { },
  mounted() { },
  methods: {

    queryDataItem() {
      DataItemManageApi.queryDataItemByCompanyMark().then(res => {
        if (res.data) this.dataItemList = res.data;
      });
    },
    queryFodder() {
      FodderApi.queryByCondition().then(res => {
        if (res.data) this.fodderList = res.data;
      });
    },
    queryFishPond() {
      FishPondApi.queryByCondition().then(res => {
        this.pondList = res.data;
      });
    },
    // 添加料管
    addPondRow(index) {
      this.formData.materialList[index].pondList.push({});
    },
    // 删除料管
    delPondRow(pondIndex, materialIndex) {
      this.formData.materialList[materialIndex].pondList.splice(pondIndex, 1);
    },
    // 添加料罐
    addMaterialRow() {
      this.formData.materialList.push({ pondList: [{}] });
    },
    // 删除料罐
    delMaterialRow() {
      this.formData.materialList.pop();
    },
    feedMachineMaterialNameBlur(e, index) {
      this.$set(
        this.formData.materialList[index],
        "feedMachineMaterialName",
        e.target.value
      );
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            FeedingEquipmentApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close();
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            console.log(this.formData);
            FeedingEquipmentApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close();
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
.input-line {
  ::v-deep .el-form-item__label {
    min-width: 140px !important;
    font-size: 14px !important;
  }

  ::v-deep .el-form .el-form-item {
    align-items: center !important;
  }
}

::v-deep .el-form-item__content {
  align-items: center;
}

.line-click {
  width: 30px;
  height: 30px;
  background-color: rgb(198, 196, 196);
  display: flex;
  justify-content: center;
  align-items: center;
  border: rgb(214, 214, 214) 1px solid;
  border-radius: 50%;
  margin-bottom: 10px;
  margin-top: 5px;
  margin-left: 20px;
  position: absolute;
  right: 0px;
  bottom: 24px;
  cursor: pointer;

  .el-icon-plus {
    color: white;
  }
}

::v-deep .divider {
  position: absolute;
  top: -25px;

  .el-divider__text {
    background-color: #f0f3f7;
    padding: 0 10px;
    font-size: 18px;
    font-weight: 400;
  }
}
</style>
